<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/service/doctor' }">医生管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加医生</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="科室名称" prop="parentId">
        <el-select v-model="ruleForm.departmentId" placeholder="请选择分类名称" style="width: 300px;">
          <el-option
              v-for="item in categoryListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="姓名" prop="doctor_id">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-input v-model="ruleForm.gender"></el-input>
      </el-form-item>

      <el-form-item label="生日" prop="birth">
        <el-date-picker
            v-model="ruleForm.birth"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="年龄" prop="description">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="treat_price">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="医生职称" prop="treat_price">
        <el-input v-model="ruleForm.professionalTitle"></el-input>
      </el-form-item>
      <el-form-item label="履历" prop="treat_price">
        <el-input v-model="ruleForm.resume"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 药品类别的下拉菜单的选项数据
      categoryListOptions: [],
      // 表单
      ruleForm: {
        department_id: '',
        name: '',
        gender:'',
        birth: '',
        age: '',
        phone: '',
        professionalTitle: '',
        resume:""
      },
      // 表单规则
      rules: {
        department_id: [
          {required: true, message: '请输入患者ID', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        gender: [
          {required: true, message: '请输入性别', trigger: 'blur'},
          {min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur'}
        ],
        birth: [
          {required: true, message: '请输入生日', trigger: 'blur'},
        ],
        age: [
          {required: true, message: '请输入医生年龄', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入电话', trigger: 'blur'},
        ],
        professionalTitle: [
          {required: true, message: '请输入医生职称', trigger: 'blur'},
        ],
        resume: [
          {required: true, message: '请输入医生履历', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    // 返回列表
    gotoList() {
      this.$router.push('./');
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9999/doctor/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
          this.axios
              .create({'headers': {'Authorization': loginInfo.token}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加医生成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: jsonResult.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 加载药品分类列表
    loadCategoryList() {
      let url = 'http://localhost:9999/departments?queryType=all';
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.categoryListOptions = jsonResult.data.list;
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
  },

  mounted() {
    this.loadCategoryList();
  }
}
</script>

<style>
.simple-link {
  color: #409eff;
  text-decoration: underline;
}
</style>